<?php

// +----------------------------------------------------------------------
// | 订单数组自定义验证器
// +----------------------------------------------------------------------
// | @author fancsen
// +----------------------------------------------------------------------
namespace app\api\validate;

use app\common\lib\exception\ApiException;

class OrderPlace extends BaseValidate
{
    protected $rule = [
		'products' => 'checkProducts',
	];

    protected $singleRule = [
        'product_id' => 'require|isPositiveInteger',
        'count'      => 'require|isPositiveInteger'
    ];

    protected function checkProducts($values)
    {
        if(!is_array($values)){
            throw new ApiException("商品参数错误", 400); 
        }

        if(empty($values)){
           throw new ApiException("商品参数不能为空", 400);  
        }

        foreach ($values as $key => $value) {
            $this->checkProduct($value);
        }
        return true;
    }

    protected function checkProduct($value)
    {
        $validate = new BaseValidate($this->singleRule);
        $result = $validate->check($value);
        if(!$result){
           throw new ApiException("商品参数错误", 400);   
        }
    }
    
}